//
//  SetViewController.m
//  SelfDriving
//
//  Created by apple on 16/1/10.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SetViewController.h"
#import "BaseButton.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation SetViewController

- (void)viewWillAppear:(BOOL)animated {
    [_table reloadData];  //xhw20181-1-29加
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    if (section == 1) {
        return 3;
    }
    if (section == 2) {
        return 2;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if(indexPath.row == 0){
            cell.textLabel.text = @"账号与安全";
            if([[AppDelegate shareDelegate].user.safe_level intValue] == 1){//简单
                cell.detailTextLabel.text = @"安全等级简单";
            }
            if([[AppDelegate shareDelegate].user.safe_level intValue] == 2){//中级
                cell.detailTextLabel.text = @"安全等级中级";
            }
            if([[AppDelegate shareDelegate].user.safe_level intValue] == 3){//高级
                cell.detailTextLabel.text = @"安全等级高级";
            }
        }
        if(indexPath.row == 1){
            cell.textLabel.text = @"账号绑定";
//            UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(SCRE_WIDTH-130, 8, 100, 30)];
            UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(SCRE_WIDTH-165, 8, 135, 30)];
            int x = 0;
//            for (int i = 0; i<3; i++) {
            for (int i = 0; i<4; i++) {
                UIImageView *logimg = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, 25, 25)];
                if (i==0) {

                    BOOL isOauth = YES;
                    if ([[AppDelegate shareDelegate].user.bind_sina isEqualToString:@"1"]) {
                        isOauth = YES;
                    }
                    else {
                        isOauth = NO;
                    }
                    logimg.image = isOauth ? [UIImage imageNamed:@"图标---微博"] : [UIImage imageNamed:@"图标未点亮---微博"];
                }else if (i==1){

                    BOOL isOauth = YES;
                    if ([[AppDelegate shareDelegate].user.bind_weixin isEqualToString:@"1"]) {
                        isOauth = YES;
                    }
                    else {
                        isOauth = NO;
                    }
                    logimg.image = isOauth ? [UIImage imageNamed:@"图标---微信"] : [UIImage imageNamed:@"图标未点亮---微信"];
                }
//                else
                else if (i==2)
                {

                    BOOL isOauth = YES;
                    if ([[AppDelegate shareDelegate].user.bind_qq isEqualToString:@"1"]) {
                        isOauth = YES;
                    }
                    else {
                        isOauth = NO;
                    }
                    logimg.image = isOauth ? [UIImage imageNamed:@"图标---qq"] : [UIImage imageNamed:@"图标未点亮---qq"];
                }
                else {
                    BOOL isOauth = YES;
                    if ([[AppDelegate shareDelegate].txl isEqualToString:@"绑定"]) {
                        isOauth = YES;
                    }
                    else {
                        isOauth = NO;
                    }
                    logimg.image = isOauth ? [UIImage imageNamed:@"通讯录---已绑定"] : [UIImage imageNamed:@"通讯录"];
                }
                [scroll addSubview:logimg];
                BaseButton *btn = [[BaseButton alloc]initWithFrame:logimg.frame];
                btn.tag = indexPath.section;
                btn.index = i;
                [btn addTarget:self action:@selector(imgTouchAction:) forControlEvents:UIControlEventTouchUpInside];
                [scroll addSubview:btn];
                x = (i+1)*35;
            }
            [cell.contentView addSubview:scroll];
            
        }
        
        
        
    }
    if (indexPath.section == 1) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if(indexPath.row == 0){
            cell.textLabel.text = @"隐身模式";
        }
        if(indexPath.row == 1){
            cell.textLabel.text = @"消息提醒";
        }
        if(indexPath.row == 2){
            cell.textLabel.text = @"默认设置更改";
        }
    }
    if (indexPath.section == 2) {
        if(indexPath.row == 0){
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = @"黑名单";
        }
        if(indexPath.row == 1){
            cell.textLabel.text = @"清理缓存";
            cell.detailTextLabel.text =  [NSString stringWithFormat:@"%.2f M",(float)[[SDImageCache sharedImageCache]getSize] / 1024 /1024];
        }
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AccountVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        
        //账号绑定
        if (indexPath.row == 1) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AccountBindingVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"StealthModeVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        if (indexPath.row == 1) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MessageAlertVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        if (indexPath.row == 2) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DefaultSettingVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        
    }
    
    
    if(indexPath.section == 2){
        if (indexPath.row == 0) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"BlackListVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        
        
        if(indexPath.row == 1){//清除缓存
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
                                                           message:@"确定清除缓存？"
                                                          delegate:self
                                                 cancelButtonTitle:@"取消"
                                                 otherButtonTitles:@"确定", nil];
            [alert show];
        }

    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDImageCache sharedImageCache] clearMemory];
        [_table reloadData];
    }
}


- (IBAction)backAction:(UIBarButtonItem *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)imgTouchAction:(BaseButton *)btn{
    
}
- (IBAction)outLoginAction:(UIButton *)sender {
    [UserDefaults clearUserDefaultWithKey:@"user"];
    //退出登录时也同时清楚第三方登录时的用户数据，添加一下代码(xhw11月1日改)
    [UserDefaults clearUserDefaultWithKey:@"ymopenfire"];
    //第一次登陆成功后，在个人中设置中退出登录后进入登录界面，清空登录界面的手机号和密码后，用户头像也要换成默认头像(xhw11月2日改)
//    if ([UserDefaults readUserDefaultObjectValueForKey:@"ruser"]) {
//        id ruser = [UserDefaults readUserDefaultObjectValueForKey:@"ruser"];
//        NSString *imgURL = [[UserDefaults readUserDefaultObjectValueForKey:@"ruser"] objectForKey:@"headsmall"];
//        if (imgURL) {
//            [ruser setObject:@"" forKey:@"headsmall"];
//            [UserDefaults writeUserDefaultObjectValue:ruser withKey:@"ruser"];
//        }
//    }
    //xhw2018-1-12改bug14881:之前是用户退出登录的时候也退出thinkChat
    [[ThinkChat instance]logOut];
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"U"
                                                                 bundle:[NSBundle mainBundle]];
    UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"LoginVC"];
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}


































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
